package dao

import (
	"Gin/app/model"
	"Gin/app/vo"
	"Gin/config"
	"Gin/utils/Mysql"
	"database/sql"
	"fmt"
	"log"
)

type UserDao struct {
}

func (u *UserDao) FindById(id int) *model.User {
	Sql := "select * from `user` where id = ?"
	db := Mysql.GetDb()
	user := &model.User{}
	err := db.QueryRow(Sql, id).Scan(&user.Id, &user.UserName, &user.PassWord, &user.TickName, &user.Times, &user.Image, &user.Ip, &user.CreateTime, &user.UpdateTime)
	if err != nil {
		fmt.Println(err.Error())
		log.Println("查询失败")
		return nil
	}
	return user
}

func (u *UserDao) FindByUserName(username string) *model.User {
	Sql := "select * from `user`  where user_name = ?"
	db := Mysql.GetDb()
	user := &model.User{}
	err := db.QueryRow(Sql, username).Scan(&user.Id, &user.UserName, &user.PassWord, &user.TickName, &user.Times, &user.Image, &user.Ip, &user.CreateTime, &user.UpdateTime)
	if err != nil {
		fmt.Println(err.Error())
		log.Println("查询失败")
		return nil
	}
	return user
}

func (u *UserDao) AddUser(usr *model.User) (int64, error) {
	Sql := "insert into `user` (user_name,pass_word,times,image,ip,create_time,update_time) values (?,?,?,?,?,?,?)"
	db := Mysql.GetDb()
	res, err := db.Exec(Sql, usr.UserName, usr.PassWord, &usr.TickName, usr.Times, usr.Image, usr.Ip, usr.CreateTime, usr.UpdateTime)
	if err != nil {
		log.Printf("添加用户失败,err:%s\n", err.Error())
		return 0, err
	}
	return res.RowsAffected()
}

func (u *UserDao) IsExistUser(username string) bool {
	Sql := "select id from `user` where user_name = ?"
	db := Mysql.GetDb()
	id := 0
	err := db.QueryRow(Sql, username).Scan(&id)
	if err != nil {
		return false
	}
	return true
}

func (u *UserDao) AddFriend(userId, friendId int) (int64, error) {
	Sql := "insert into `friend` (user_id,friend_id) values (?,?)"
	db := Mysql.GetDb()
	exec, err := db.Exec(Sql, userId, friendId)
	_, _ = db.Exec(Sql, friendId, userId)
	if err != nil {
		log.Println("添加失败,", err.Error())
		return 0, err
	}
	return exec.RowsAffected()
}

func (u *UserDao) GetUserInfo(userId int) (tick, icon string) {
	Sql := "select tick_name,image from `user` where id = ?"
	db := Mysql.GetDb()
	_ = db.QueryRow(Sql, userId).Scan(&tick, &icon)
	return
}

func (u *UserDao) FindWithNike(nick string) *vo.UserVo {
	Sql := "select id,tick_name,image from `user` where tick_name= %s "
	db := Mysql.GetDb()
	target := &vo.UserVo{}
	err := db.QueryRow(fmt.Sprintf(Sql, nick)).Scan(&target.Id, &target.TickName, &target.Image)
	if err != nil {
		return nil
	}
	return target
}

func (u *UserDao) GetFriendList(userId int) ([]*vo.UserVo, error) {
	Sql := "select id,tick_name,image from `user`,`friend` where user.id=`friend`.friend_id and `friend`.user_id=?"
	db := Mysql.GetDb()
	rows, err := db.Query(Sql, userId)
	if err != nil {
		log.Println("获取好友列表失败")
	}
	defer func(rows *sql.Rows) {
		err := rows.Close()
		if err != nil {
			log.Println("close err,", err.Error())
		}
	}(rows)
	var friendList []*vo.UserVo
	for rows.Next() {
		friend := &vo.UserVo{}
		err := rows.Scan(&friend.Id, &friend.TickName, &friend.Image)
		if err != nil {
			log.Println("获取好友列表失败", err.Error())
			return nil, config.GetFriendListError
		}
		friendList = append(friendList, friend)
	}
	return friendList, nil
}
